<script setup>
import Info from './info.vue'
import { useConfirm } from '@/hooks/useConfirm'
// import mock from '~mock/menu'
import {onMounted, ref} from 'vue'
import {pageApi,deleteApi} from "../../../api/coursetype";
import {ElMessage, ElMessageBox} from "element-plus";

const tableData = ref({})

// 表单状态
const filterState = ref(true)
// 表单数据 -搜索条件
const formData = ref({
  page: 1,
  size: 10,
  name: ''
})

onMounted(()=>{
  loadData()
})

const loadData=()=>{
  pageApi(formData.value).then(r=>{
    tableData.value=r.data;
  })
}
// 编辑
const authEditRef = ref()
const onAuthEdit = (row) => {
  authEditRef.value.state = true
  // 回显
  if (row) {
    authEditRef.value.formData.id = row.id
    authEditRef.value.formData.name = row.name
    authEditRef.value.formData.info = row.info
  }
}

// 权限删除
const onDelete = (row) => {
  ElMessageBox.confirm('确定删除该条数据吗?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    //接口请求 删除角色
    const  r=await deleteApi({id:row.id});
    if(r.code==0){
      ElMessage({
        type: 'success',
        message: '删除成功'})
        //重新加载请求
        loadData()
      }else {
        ElMessage.error(r.data)
      }
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '用户取消'
    })
  })
}

</script>
<template>
  <Box>
    <!-- 逻辑层 -->
    <Info ref="authEditRef" @flush="loadData"/>
    <!-- 逻辑层 -->

    <!-- 筛选 -->
    <template #filter v-if="filterState">
      <el-form :inline="true" :model="formData">
        <el-form-item label="类型名称">
          <el-input placeholder="请输入类型名称" v-model="formData.name" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadData"> <span class="iconfont icon-sousuo"></span>搜索 </el-button>
          <el-button type="default"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </template>
    <!-- 筛选 -->

    <!-- 按钮组 -->
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="onAuthEdit">
          <span class="iconfont icon-jiahao"></span>添加
        </el-button>
      </el-col>

      <el-col :span="12" style="text-align: right">
        <el-button circle @click="filterState = !filterState">
          <span class="iconfont icon-sousuo"></span>
        </el-button>
        <el-button circle>
          <span class="iconfont icon-shuaxin"></span>
        </el-button>
      </el-col>
    </el-row>
    <!-- 按钮组 -->

    <el-table :data="tableData.data" :border="true" class="qfTable" row-key="id" default-expand-all>
      <el-table-column label="序号" prop="id" align="left"></el-table-column>
      <el-table-column label="名称" prop="name" align="left"></el-table-column>
      <el-table-column label="说明信息" prop="info" align="left"></el-table-column>
      <el-table-column label="创建时间" prop="ctime" align="left"></el-table-column>

      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template #default="scoped">
          <el-button type="primary" link size="small" @click="onAuthEdit(scoped.row)">
            <span class="iconfont icon-bianji"></span>修改
          </el-button>
          <el-button type="primary" link size="small" @click="onDelete(scoped.row)">
            <span class="iconfont icon-shanchu"></span>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格 -->
    <!-- 分页 -->
    <el-pagination
        v-model:current-page="formData.page"
        v-model:page-size="formData.size"
        :page-sizes="[10, 20, 30, 40]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        @size-change="loadData"
        @current-change="loadData"
    />

  </Box>
</template>

<style lang="scss" scoped></style>
